import json
import random

import pandas as pd
import requests


def get_rucang(start_time, end_time):
    with open("../../jingdong_order_synchronization_kuaipu_zhuzhanghao/auth/jingdong_cookies.json", "r") as f:
        cookie_data = json.load(f)
        JSESSIONID = [cookie["value"] for cookie in cookie_data if cookie["name"] == "JSESSIONID"]
        flash = [cookie["value"] for cookie in cookie_data if cookie["name"] == "flash"]
        jingdong_cookie = f"flash={random.choice(flash)};JSESSIONID={random.choice(JSESSIONID)};"
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
        "cookie": jingdong_cookie,
    }
    print(headers)
    payload = [{
        "page": 1,
        "pageSize": 200,
        "poCreateDateEnd": start_time,
        "poCreateDateStart": end_time,
        "queryInboundType": 1,
        "source": "2",
        "sourceId": "3",
        "stateList": ["3"],
        "yn": "1"
    }]
    print(payload)
    url = 'https://procurement.jd.com/api/procurement/poQuery/queryPurchaseOrderList'
    response = requests.post(url, headers=headers, json=json.load(payload))
    datas = response.json()["data"]["data"]
    print(datas)
    rucang_totalPrice = 0
    for data in datas:
        print(data)
        rucang_totalPrice += data["totalPrice"]
    return rucang_totalPrice
get_rucang("2025-03-01 00:00:00","2025-03-20 23:59:59")